<?php

declare(strict_types=1);

namespace App\Controller\store;

use App\Middleware\User\StoreMiddleware;
use App\Request\Store\Withdraw\DetailRequest;
use App\Service\Store\WithdrawService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Middleware;

/**
 * 后台提现管理
 * @AutoController()
 * @Middleware(StoreMiddleware::class)
 * @author 陈奕文
 * @package App\Controller\store
 */
class WithdrawController extends Controller
{

    /**
     * 提现服务
     * @Inject()
     * @var WithdrawService
     */
    private $withdraw_service;

    /**
     * 提现搜索
     */
    public function search()
    {
        $params = $this->request->all();
        // 获取用户提现列表
        $user_cash_list = $this->withdraw_service->getUserCashList($params);

        $this->success($user_cash_list);

    }

    /**
     * 获得提现详细信息
     */
    public function detailInfo()
    {
        $id = (int)$this->request->input('id');
        $cash = $this->withdraw_service->dao->first($id, ['id', 'user_id', 'order_no', 'amount', 'bank_name',
            'bank_account', 'bank_realname', 'bank_idcard', 'bank_phone', 'remark', 'status',
            'refusal_reason', 'admin_id', 'processing_time', 'start_time']);
        !$cash && $this->error("不存在的提现记录");
        $cash = $cash->toArray();

        $user = $this->withdraw_service->user_dao->first($cash['user_id']);
        !$user && $this->error("不存在提现用户");
        $user = $user->toArray();

        $cash['name'] = $user['name']; // 昵称
        $cash['phone'] = $user['phone']; // 手机号
        !empty($cash['processing_time']) && $cash['processing_time'] = date("Y-m-d H:i:s", (int)$cash['processing_time']);

        $this->success($cash);
    }

    /**
     * 提现审核提交
     * @param DetailRequest $request
     */
    public function detail(DetailRequest $request)
    {
        $id = (int)$request->input('id'); // id
        $status = (int)$request->input('status'); // 审核状态
        $refusal_reason = $request->input('refusal_reason', ''); // 拒绝理由

        $result = $this->withdraw_service->dao->update($id,[
            'status' => $status,
            'refusal_reason' => $refusal_reason,
            'processing_time' => time(),
            'admin_id' => storeId(),
        ]);

        !$result ? $this->error()  : $this->success();
    }

}
